<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="../day35/data.js"></script>
    <style>
      * {
        margin: 0;
        padding: 0;
        list-style: none;
      }

      .header,
      .footer {
        width: 1200px;
        height: 80px;
        margin: 0 auto;
        display: flex;
        background-color: skyblue;
        justify-content: center;
        align-items: center;
        font-size: 40px;
        color: #fff;
      }

      .content {
        width: 1200px;
        padding: 10px 0;
        margin: 0 auto;
      }

      ul {
        display: flex;
        flex-wrap: wrap;
        justify-content: space-around;
      }

      li {
        width: 280px;
        border: 1px solid #333;
        padding-bottom: 10px;
        margin-bottom: 10px;
      }

      li > img {
        width: 100%;
        height: 320px;
        display: block;
      }

      li > p {
        padding-left: 10px;
        padding-top: 10px;
      }
      .sle {
        width: 60px;
        height: 35px;
        font-size: 30px;
        text-align: center;
        vertical-align: middle;
      }
      .sleo {
        width: 60px;
        height: 35px;
        font-size: 30px;
        text-align: center;
        vertical-align: middle;
      }
      .press {
        margin-left: 120px;
        margin-top: 10px;
      }
      button {
        font-size: 30px;
        background-color: rgb(194, 211, 182);
        width: 50px;
        /* 中线对齐 */
        vertical-align: middle;
      }
      select {
        font-size: 30px;
        background-color: rgb(194, 211, 182);
        width: 70px;
        text-align: center;
        /* 中线对齐 */
        vertical-align: middle;
      }
      span {
        font-size: 30px;
        vertical-align: middle;
      }
    </style>
  </head>
  <body>
    <div class="header">网页头部</div>
    <div class="press">
      <button class="leftBtn">&lt;</button>
      <input type="" class="sle" value="1" />
      <span>/</span>
      <input type="text" class="sleo" value="7" disabled/>
      <button class="rightBtn">&gt;</button>
      <select>
        <option value="4">4</option>
        <option value="6">6</option>
        <option value="8">8</option>
        <option value="12">12</option>
      </select>
    </div>
    <div class="content">
      <ul>
        <li>
          <img
            src="https://img.alicdn.com/bao/uploaded/https://img.alicdn.com/imgextra/i3/2251059038/O1CN01zJEpaN2GdSG1Xvo8y_!!2251059038.jpg"
            alt=""
          />
          <p>2020西太湖国际音乐节</p>
          <p>城市：常州</p>
          <p>地址: 常州西太湖中国花卉博览景区北门</p>
          <p>价格: 199-399</p>
          <p>演出时间: 2020.09.19-09.20</p>
        </li>
      </ul>
    </div>
    <div class="footer">网页底部</div>

    <script>
      // 实现 通过 data.js 渲染到页面

      // 0. 获取元素
      let ul = document.querySelector("ul");
      let leftBtn = document.querySelector(".leftBtn");
      let rightBtn = document.querySelector(".rightBtn");
      let ipt = document.querySelector(".sle");
      let select = document.querySelector("select");
      let ipt2 = document.querySelector(".sleo");

      //select 下拉菜单
    //   let pages = Math.ceil(list.length / select.value);
      //新的变量等于下拉菜单的值
      let page = ipt.value;
      let selectNum = select.value - 0;
      

      leftBtn.disabled = true;

      //   右边按钮
      rightBtn.onclick = function () {
        //点右边的时候左边➡️能动
        leftBtn.disabled = false;
        page++;
        ipt.value = page;
        if (page == ipt2.value) {
          rightBtn.disabled = true;
        } else {
          rightBtn.disabled = false;
        }
        fn(page, selectNum);
        cursor();
        // console.log(page)
      };
      //  左边按钮

      leftBtn.onclick = function () {
        //点击左边的时候右边➡️能动
        rightBtn.disabled = false;
        page--;
        ipt.value = page;
        if (page == 1) {
          leftBtn.disabled = true;
        } else {
          leftBtn.disabled = false;
        }
        fn(page, selectNum);
        cursor();
      };


      select.onchange = function () {
        selectNum = select.value - 0;
        select.value = selectNum;
        ipt2.value =  Math.ceil(list.length / select.value);
        if (page < ipt2.value){
          fn(page,selectNum);
          rightBtn.disabled = false;
          rightBtn.style.cursor = "pointer";
        }else if(page == ipt2.value){
          rightBtn.disabled = true;
          rightBtn.style.cursor = "not-allowed";
          fn(page,selectNum);
        }else{
          page = 1;
          ipt.value = page;
          console.log(page);
          rightBtn.disabled = false;
          rightBtn.style.cursor = "pointer";
          fn(page,selectNum);
        }
      };


      //鼠标样式
      cursor();
      function cursor() {
        if (page == 1) {
          leftBtn.style.cursor = "not-allowed";
        } else if (page == 7) {
          rightBtn.style.cursor = "not-allowed";
        } else {
          leftBtn.style.cursor = "pointer";
          rightBtn.style.cursor = "pointer";
        }
      }

      fn(page, selectNum);
      function fn(a, b) {
        // 1. 准备一个原始字符串，用来接收遍历后的结果
        let str = "";
        let start = (a - 1) * b; //开始索引
        let over = b + start; //结束索引
        //截取哪个的数据
        let shot = list.slice(start, over);
        // 2. 遍历data.js中截取后的list；
        shot.forEach(function (item) {
          // item就是list中的每一个对象
          // 有多少项，这个函数就要执行多少次
          // 有多少项，就需要多少个li
          str += `
                <li>
                    <img src="${item.pic}"/>
                    <p>${item.name}</p>
                    <p>城市：${item.city}</p>
                    <p>地址: ${item.address}</p>
                    <p>价格: ${item.price}</p>
                    <p>演出时间: ${item.showTime}</p>
                </li>
            `;
        });
        ul.innerHTML = str;
      }
    </script>
  </body>
</html>
